package com.zhang.sparksql_2

import org.apache.spark.sql.SparkSession

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/19 09:36 
 */
object SparkSQL04_seq {

  def main(args: Array[String]): Unit = {
    System.setProperty("HADOOP_USER_NAME", "zhang")

    //todo 获取执行环境
    val spark: SparkSession = SparkSession
      .builder()
      .enableHiveSupport()
      .master("local[*]")
      .appName("DataFrame")
      .getOrCreate()

    spark.sql(
      """
        |CREATE TABLE `user_visit_action`(
        |  `date` string,
        |  `user_id` bigint,
        |  `session_id` string,
        |  `page_id` bigint,
        |  `action_time` string,
        |  `search_keyword` string,
        |  `click_category_id` bigint,
        |  `click_product_id` bigint,
        |  `order_category_ids` string,
        |  `order_product_ids` string,
        |  `pay_category_ids` string,
        |  `pay_product_ids` string,
        |  `city_id` bigint)
        |row format delimited fields terminated by '\t'
        |""".stripMargin)

    spark.sql(
      """
        |load data local inpath 'input/user_visit_action.txt' into table user_visit_action
        |""".stripMargin)

    spark.sql(
      """
        |CREATE TABLE `product_info`(
        |  `product_id` bigint,
        |  `product_name` string,
        |  `extend_info` string)
        |row format delimited fields terminated by '\t'
        |""".stripMargin)

    spark.sql(
      """
        |load data local inpath 'input/product_info.txt' into table product_info
        |""".stripMargin)


    spark.sql(
      """
        |CREATE TABLE `city_info`(
        |  `city_id` bigint,
        |  `city_name` string,
        |  `area` string)
        |row format delimited fields terminated by '\t'
        |""".stripMargin)

    spark.sql(
      """
        |load data local inpath 'input/city_info.txt' into table city_info
        |""".stripMargin)

    spark.sql("select * from city_info").show()

    // todo 关闭环境
    spark.stop()

  }
}
